package com.zhang.activity;

import android.R.integer;
import android.content.Context;
import android.graphics.Point;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * 目前实现的效果：可以左右拖拽. 目标效果:向左拖拽，释放时如果移动到距父控件左边距1/4距离是自动吸附至最左端。反之吸附至最右端
 * 
 * @author zhangshichen
 * 
 */
public class VDHLayout extends LinearLayout {
	private ViewDragHelper mDragger;
	private int resultTag=2;//0,表示确定，1，表示取消，2，表示没有选
	private View mDragView;
	/*
	 * private View mAutoBackView; private View mEdgeTrackerView;
	 */

	private Point mAutoBackOriginPos = new Point();

	public VDHLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mDragger = ViewDragHelper.create(this, 1.0f,
				new ViewDragHelper.Callback() {
					@Override
					public boolean tryCaptureView(View child, int pointerId) {
						// mEdgeTrackerView禁止直接移动
						return child == mDragView /* || child == mAutoBackView */;
					}

					// 限制横向可拖拽范围
					@Override
					public int clampViewPositionHorizontal(View child,
							int left, int dx) {
						final int leftBound = getPaddingLeft();
						final int rightBound = getWidth()
								- mDragView.getWidth() - leftBound;

						final int newLeft = Math.min(Math.max(left, leftBound),
								rightBound);

						return newLeft;
					}

					// 限制纵向可拖拽范围
					@Override
					public int clampViewPositionVertical(View child, int top,
							int dy) {
						final int topBound = getPaddingTop();
						final int bottomBound = getHeight()
								- mDragView.getHeight() - topBound;
						final int newTop = Math.min(Math.max(top, topBound),
								bottomBound);
						return newTop;
					}

					// 手指释放的时候回调

					@Override
					public void onViewReleased(View releasedChild, float xvel,
							float yvel) { // mAutoBackView手指释放时可以自动回去
						if (releasedChild == mDragView) {
							if (releasedChild.getLeft()<(getWidth()/4)) {
								//显示到最左端，确定
								mAutoBackOriginPos.x=0;
								resultTag=0;
							}else if (releasedChild.getRight()>(getWidth()/4*3)) {
								//显示到最右端，取消
								mAutoBackOriginPos.x=getWidth()-releasedChild.getWidth();
								resultTag=1;
							}else {
								resultTag=2;
							}
							//不满足以上两个条件则回到初始位置
							mDragger.settleCapturedViewAt(mAutoBackOriginPos.x,
									mAutoBackOriginPos.y);
							invalidate();
						}
					}

					// 在边界拖动时回调
					/*
					 * @Override public void onEdgeDragStarted(int edgeFlags,
					 * int pointerId) {
					 * mDragger.captureChildView(mEdgeTrackerView, pointerId); }
					 */
				});
		mDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent event) {
		return mDragger.shouldInterceptTouchEvent(event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mDragger.processTouchEvent(event);
		return true;
	}

	@Override
	public void computeScroll() {
		if (mDragger.continueSettling(true)) {
			invalidate();
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);

		mAutoBackOriginPos.x = mDragView.getLeft();
		mAutoBackOriginPos.y = mDragView.getTop();
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();

		mDragView = getChildAt(0);
		/*
		 * mAutoBackView = getChildAt(1); mEdgeTrackerView = getChildAt(2);
		 */
	}
	
	public int getResultTag(){
		return resultTag;
	}
}
